import { expect } from '@jest/globals';
import * as compressing from 'compressing';
import fsExtra from 'fs-extra';
import JSON5 from 'json5';
import path from 'path';

import { initExecuteCommand, runHvigor } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility
  },
  modulesOption: {}
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  const projectRoot = project.projectRootPath();
  const entryModulePath = project.getModule('entry').moduleRootPath();

  const buildProfile = await fsExtra.readFile(path.resolve(projectRoot, 'build-profile.json5'));
  const buildProfileJson = JSON5.parse(buildProfile.toString());

  const productExist = buildProfileJson.app.products.length === 1;
  const targetExist = buildProfileJson.modules[0].targets.length === 1;

  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHap'
  });
  const result = runHvigor(projectRoot, commands);
  record.addStep('执行hap编译构建', result);

  const entryHapExist =
    await fsExtra.pathExists(path.resolve(entryModulePath, 'build/default/outputs/default/entry-default-unsigned.hap'));

  const commandsApp = initExecuteCommand({
    mode: 'project',
    product: 'default',
    tasks: 'assembleApp'
  });
  const resultApp = runHvigor(projectRoot, commandsApp);
  record.addStep('执行app编译构建', resultApp);

  await fsExtra.copyFile(path.resolve(projectRoot, `build/outputs/default/${project.name}-default-unsigned.app`),
    path.resolve(projectRoot, `build/outputs/default/${project.name}-default-unsigned.zip`));
  // eslint-disable-next-line max-len
  await compressing.zip.uncompress(path.resolve(projectRoot, `build/outputs/default/${project.name}-default-unsigned.zip`),
    path.resolve(projectRoot, `build/outputs/default/${project.name}-default-unsigned`));
  const incrementHapExist =
    await fsExtra.pathExists(path.resolve(projectRoot, `build/outputs/default/${project.name}-default-unsigned/entry-default.hap`));

  expect(productExist).toBe(true);
  expect(targetExist).toBe(true);
  expect(entryHapExist).toBe(true);
  expect(incrementHapExist).toBe(true);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
  expect(resultApp.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('OPENLAB_AIDE_ZOS_Build_Visualization_Target_HOS_api10_Stage_Single_Module_001')
  .description('api10-Stage单模块工程，1个product，1个target，product包含1个target')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.defaultOpt())
  .testFun(testFun)
  .load();
